home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2526 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.3 KB  |  56 lines

  1. Newsgroups: comp.lang.c++
  2. Path: newshub.ariel.cs.yorku.ca!cs911108
  3. From: cs911108@ariel.cs.yorku.ca (IAN J COLOMBY)
  4. Subject: Function overloading problem
  5. X-Nntp-Posting-Host: red
  6. Message-ID: <DLBxoH.GLw@ariel.cs.yorku.ca>
  7. Sender: news@ariel.cs.yorku.ca (*)
  8. Organization: York University, Dept. of Computer Science
  9. Date: Wed, 17 Jan 1996 14:31:28 GMT
  10.  
  11. I have the following code which in which the function call of f() inside 
  12. function g() doesn't behave the way I expected it to.
  13.  
  14. class B;
  15. class C;
  16.  
  17. class A
  18. {
  19.    virtual void f(B*);
  20.    virtual void f(C*);
  21. };
  22.  
  23. class B: public A
  24. {
  25.    void f(B*);
  26.    void f(C*);
  27. };
  28.  
  29. class C: public A
  30. {
  31.    void f(B*);
  32.    void f(C*);
  33. };
  34.  
  35.  
  36. void g(A* a1, A* a2)
  37. {
  38.     a1->f(a2);
  39. }
  40.  
  41. The above code first of all won't compile unless I insert the following 
  42. line of code into each class definition:
  43.     vitrual void f(A*) in class A or void f(A*) in classes B & C.
  44.  
  45. The next problem is that the above line f(A*) is the function that always 
  46. gets called in the class of a1 no matter what a2 is in function g().  a1 
  47. and a2 will never be of class A, they will only be of either class B or 
  48. class C, so I'm not sure why the the call a1->f(a2) always calls the 
  49. function f(A*) in the class of a1, and not f(B*) or f(C*) depending on 
  50. what class a2 is.
  51.  
  52. Any help in solving this problem would be appreciated.
  53.  
  54. Ian.
  55.  
  56.